package cn.stylefeng.guns.modular.ry600.service.impl;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.stylefeng.guns.core.util.CacheUtil;
import cn.stylefeng.guns.core.util.Token;
import cn.stylefeng.guns.modular.ry600.service.IClientService;
import cn.stylefeng.guns.modular.system.dao.ClientMapper;
import cn.stylefeng.guns.modular.system.model.Client;
import cn.stylefeng.guns.modular.system.model.ClientCheck;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author stylefeng
 * @since 2018-12-06
 */
@Service
public class ClientServiceImpl extends ServiceImpl<ClientMapper, Client> implements IClientService {
	@Autowired
	private ClientMapper clientMapper;
	
	@Override
	public Map<String,Object> checkToken(ClientCheck clientCheck) {
		Map<String,Object> search=new HashMap<String,Object>();
		search.put("client_id",clientCheck.getClient_id());
		List<Client> clientList= this.selectByMap(search);
		if(clientList.size()>0){
			for(int i=0;i<clientList.size();i++){
				if(clientCheck.getClient_secret().equals(clientList.get(i).getClientSecret())){
					//生成token
					String token=Token.getToken(clientCheck.getClient_id());
					//缓存token
//					CacheUtil.put(clientCheck.getClient_id(), clientCheck.getClient_id(), token);
					CacheUtil.put("bearer "+token,"bearer "+token,"bearer "+token);
					System.out.println("客户存在");
					Map<String,Object> resultToken=new HashMap<String,Object>();
					resultToken.put("access_token", token);
					resultToken.put("expires_in", 3600);
					return resultToken;
				}
			}
		}
		//System.out.println(CacheUtil.get(clientCheck.getClient_id(), clientCheck.getClient_id()));
		System.out.println("客户不存在");
		return null;
	}

	@Override
	public Object selectByMsg(Map<String, Object> msg) {
		if (MapUtils.isNotEmpty(msg)) {
			String[] likeFields = { "clientName"};
			for (String field : likeFields) {
				if (msg.containsKey(field)) {
					msg.put(field, "%" + (String) msg.get(field) + "%");
				}
			}
		}
		
		return clientMapper.selectByMsg(msg);
	}

}
